<!DOCTYPE html>
<!-- saved from url=(0014)about:internet -->
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link rel="stylesheet" href="./login_files/components.chunk.css">
    <link rel="stylesheet" href="./login_files/umi.css">

    <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,minimum-scale=1,user-scalable=no">
    <meta name="theme-color" content="#343a40">

    <title>汇文投标施组</title>
    <link rel="icon" href="/logo.png" type="image/x-icon" />
    <!-- <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Nunito+Sans:300,400,400i,600,700"> -->
    <script>window.routerBase = "/";</script>
    <script>
        window.settings = {
            title: '汇文投标施组',
            assets_path: '/theme/v2board/assets',
            theme: {
                sidebar: 'dark',
                header: 'dark',
                color: 'black',
            },
            version: '1.7.4.1681103823832',
            background_url: '',
            description: '欢迎！请登录',
            logo: ''
        }
    </script>
    <script src="toast-jquery-3.4.1.js"></script>
    <link rel="stylesheet" type="text/css" href="toast.css">
    <script src="toast.js"></script>
    <script src="config.js"></script>
    <link rel="stylesheet" href="./login_files/black.css">
</head>

<body>
    <div id="root">
        <div id="page-container">
            <main id="main-container">
                <div class="v2board-background"></div>
                <div class="no-gutters v2board-auth-box">
                    <div style="max-width: 450px; width: 100%; margin: auto;">
                        <div class="mx-2 mx-sm-0">
                            <div class="block block-rounded block-transparent block-fx-pop w-100 mb-0 overflow-hidden bg-image"
                                style="box-shadow: rgba(0, 0, 0, 0.05) 0px 0.5rem 2rem;">
                                <div class="row no-gutters">
                                    <div class="col-md-12 order-md-1 bg-white">
                                        <div class="block-content block-content-full px-lg-4 py-md-4 py-lg-4">
                                            <div class="mb-3 text-center"><a class="font-size-h1"
                                                    href="javascript:void(0);"><span id="title"
                                                        class="text-dark">汇文投标施组</span></a>
                                                <p id="p-info" class="font-size-sm text-muted mb-3">欢迎！请登录</p>
                                            </div>
                                            <div class="form-group"><input id="long-input1" type="text"
                                                    class="form-control form-control-alt" placeholder="邮箱"></div>


                                            <div id="div-send" class="form-group"><input id="input-send" type="text"
                                                    class="form-control form-control-alt" placeholder="验证码"><button
                                                    style="margin-top: 7px;" id="btn-send" type="submit"
                                                    class="btn btn-block btn-primary "><span>获取验证码</span></button></div>

                                            <div class="form-group"><input id="long-input2" type="password"
                                                    class="form-control form-control-alt" placeholder="密码"></div>

                                            <div id="div-confirm" class="form-group"><input id="long-input3"
                                                    type="password" class="form-control form-control-alt"
                                                    placeholder="确认密码"></div>

                                            <div class="form-group mb-0"><button id="action-btn" type="submit"
                                                    class="btn btn-block btn-primary"><span>登陆</span></button>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div id="div-menu" class="text-left bg-gray-lighter p-3 px-4"><a
                                        class="font-size-sm text-muted" href="#" onclick="showReg();">注册</a>
                                    <div class="ant-divider ant-divider-vertical" role="separator"></div><a
                                        class="font-size-sm text-muted" href="#" onclick="findPsw();">忘记密码</a>
                                </div>
                                <div id="div-back" style="display:none" class="text-left bg-gray-lighter p-3 px-4"><a
                                        class="font-size-sm text-muted" href="#" onclick="showLogin();">返回</a>
                                </div>

                            </div>
                        </div>
                    </div>
                </div>
            </main>
        </div>
    </div>

    <!-- <script src="./login_files/vendors.async.js"></script>
    <script src="./login_files/components.async.js"></script>
    <script src="./login_files/umi.js"></script> -->

    <script>
        toastr.options = {
            // toastr配置
            "closeButton": true, //是否显示关闭按钮
            "debug": false, //是否使用debug模式
            "progressBar": true, //是否显示进度条，当为false时候不显示；当为true时候，显示进度条，当进度条缩短到0时候，消息通知弹窗消失
            "preventDuplicates": true,
            "positionClass": "toast-top-center",//显示的动画位置
            "showDuration": "400", //显示的动画时间
            "hideDuration": "1000", //消失的动画时间
            "timeOut": "7000", //展现时间
            "extendedTimeOut": "1000", //加长展示时间
            "showEasing": "swing", //显示时的动画缓冲方式
            "hideEasing": "linear", //消失时的动画缓冲方式
            "showMethod": "fadeIn", //显示时的动画方式
            "hideMethod": "fadeOut" //消失时的动画方式
        }
        var title = document.getElementById('title')
        var info = document.getElementById('p-info')
        var input1 = document.getElementById('long-input1')
        var input2 = document.getElementById('long-input2')
        var input3 = document.getElementById('long-input3')
        var action_btn = document.getElementById('action-btn')

        var div_send = document.getElementById('div-send')
        var div_confirm = document.getElementById('div-confirm')

        var input_send = document.getElementById('input-send')
        var btn_send = document.getElementById('btn-send')

        var div_menu = document.getElementById('div-menu')
        var div_back = document.getElementById('div-back')

        function showLogin() {
            title.innerHTML = "汇文投标施组"
            info.innerHTML = "欢迎！请登录"
            action_btn.innerHTML = "登录"
            div_send.style.display = 'none'
            div_confirm.style.display = 'none'
            div_menu.style.display = 'block'
            div_back.style.display = 'none'
            action_btn.onclick = () => {
                var name = input1.value
                var p = input2.value
                const params = {
                    username: name,
                    psw: p
                }
                const queryString = Object.keys(params)
                    .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`)
                    .join('&')
                const xhr = new XMLHttpRequest()
                xhr.open('POST', baseUrl + '/login')
                xhr.onload = async function () {
                    var res = JSON.parse(xhr.response);
                    console.log(res)
                    if (res.code === 200) {
                        toastr.clear()
                        toastr.success("登录成功")
                        var tokenInfo = {
                            username: name,
                            admin: res.admin,
                            token: res.token
                        }
                        localStorage.setItem('tokenInfo', JSON.stringify(tokenInfo))
                        window.location.href = "./gen.html";
                    } else {
                        toastr.clear()
                        toastr.error(res.msg)
                    }
                }
                xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
                xhr.send(queryString);
            }
        }

        function showReg() {
            title.innerHTML = "账号注册"
            info.innerHTML = "欢迎！请注册"
            action_btn.innerHTML = "注册"
            div_send.style.display = 'none'
            div_confirm.style.display = 'block'
            div_menu.style.display = 'none'
            div_back.style.display = 'block'
            action_btn.onclick = () => {
                var name = input1.value
                var p = input2.value
                var r = input3.value
                if (p !== r) {
                    toastr.clear()
                    toastr.error("两次密码输入不一致")
                    return
                }
                const params = {
                    username: name,
                    psw: p
                }
                const queryString = Object.keys(params)
                    .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`)
                    .join('&')
                const xhr = new XMLHttpRequest()
                xhr.open('POST', baseUrl + '/register')
                xhr.onload = async function () {
                    var res = JSON.parse(xhr.response);
                    console.log(res)
                    if (res.code === 200) {
                        toastr.clear()
                        toastr.success("注册成功,请登录")
                        showLogin()
                    } else {
                        toastr.clear()
                        toastr.error(res.msg)
                    }
                }
                xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
                xhr.send(queryString);
            }
        }


        function isEmail(str) {
            var string = str.replace(/\s|&nbsp;/g, '')
            var reg = /^[a-zA-Z0-9_-]+@([a-zA-Z0-9]+\.)+(com|cn|net|org)$/;
            let isEmail = reg.test(string)
            return isEmail
        }

        function findPsw() {
            title.innerHTML = "找回密码"
            info.innerHTML = ""
            action_btn.innerHTML = "重置密码"
            div_send.style.display = 'block'
            div_confirm.style.display = 'block'
            div_menu.style.display = 'none'
            div_back.style.display = 'block'
            // get email code
            btn_send.onclick = () => {
                toastr.clear()
                let email = input1.value
                if (!isEmail(email)) {
                    toastr.error("email格式不正确，请重新输入")
                    return
                }
                btn_send.disabled = true;
                let waitSec = 30;
                function countdown() {
                    if (waitSec === 0) {
                        btn_send.innerHTML = '获取验证码';
                        btn_send.disabled = false;
                    } else {
                        btn_send.innerHTML = '获取验证码(' + waitSec + 's)';
                        waitSec--;
                        setTimeout(countdown, 1000);
                    }
                }
                countdown();

                let params = {
                    username: email
                }
                const queryString = JSON.stringify(params)
                const xhr = new XMLHttpRequest()
                xhr.open('POST', '/getEmailCode')
                xhr.setRequestHeader('Content-Type', 'application/json')
                xhr.onload = async function () {
                    var res = JSON.parse(xhr.response);
                    console.log(res)
                    if (res.code === 200) {
                        toastr.clear()
                        toastr.success(res.msg)
                    } else {
                        toastr.clear()
                        toastr.error(res.msg)
                    }
                }
                xhr.send(queryString);
            }
            action_btn.onclick = () => {
                toastr.clear()
                let email = input1.value.trim()
                let code = input_send.value.trim()
                let psw1 = input2.value
                let psw2 = input3.value
                if (psw1 !== psw2) {
                    toastr.error("两次密码输入不一致，请重新输入")
                    return
                }
                if (!isEmail(email)) {
                    toastr.error("email格式不正确，请重新输入")
                    return
                }
                if (code.length !== 6) {
                    toastr.error("验证码格式不正确")
                    return
                }
                let params = {
                    username: email,
                    psw: psw1,
                    emailCode: code
                }
                const queryString = JSON.stringify(params)
                const xhr = new XMLHttpRequest()
                xhr.open('POST', '/resetPsw')
                xhr.setRequestHeader('Content-Type', 'application/json')
                xhr.onload = async function () {
                    var res = JSON.parse(xhr.response);
                    console.log(res)
                    if (res.code === 200) {
                        toastr.clear()
                        toastr.success(res.msg)
                        showLogin()
                    } else {
                        toastr.clear()
                        toastr.error(res.msg)
                    }
                }
                xhr.send(queryString);
            }
        }

        showLogin()
        // check token
        var tokenInfo = localStorage.getItem('tokenInfo')
        if (tokenInfo) {
            var params = JSON.parse(tokenInfo)
            const queryString = Object.keys(params)
                .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`)
                .join('&')
            const xhr = new XMLHttpRequest()
            xhr.open('POST', baseUrl + '/checkToken')
            xhr.onload = async function () {
                var res = JSON.parse(xhr.response);
                console.log(res)
                if (res.code === 200) {
                    toastr.clear()
                    console.log("验证token成功")
                    if (res.token) {
                        var tokenInfo = {
                            username: name,
                            token: res.token
                        }
                        localStorage.setItem('tokenInfo', JSON.stringify(tokenInfo))
                    }
                    window.location.href = "./gen.html"
                }
            }
            xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
            xhr.send(queryString);
        } 
    </script>

</body>

</html>